package com.mvc.business.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.business.model.OrderItem;
import com.mvc.business.model.PackingItem;
import com.mvc.business.model.PurchaseItem;
import com.mvc.finance.model.CommercialInvoiceItem;
import com.mvc.finance.service.CommercialInvoiceItemManager;
import com.mvc.framework.model.Auditable;
import com.mvc.framework.service.BaseService;

@Service
public class OrderItemManager extends BaseService<OrderItem, Integer> {

	@Autowired
	private OrderManager	             orderManager;

	@Autowired
	private PurchaseItemManager	         purchaseItemManager;

	@Autowired
	private PackingItemManager	         packingItemManager;

	@Autowired
	private CommercialInvoiceItemManager	commercialInvoiceItemManager;

	@Autowired
	private OrderAssociatedManager	     orderAssociatedManager;

	public List<OrderItem> getOrderItemsByOrderId(int orderId) {
		OrderItem orderItem = new OrderItem();
		orderItem.setOrderId(orderId);
		orderItem.setRecStatus(Auditable.REC_STATUS_VALID);
		return getHibernateTemplate().findByExample(orderItem);
	}

	public void deleteByOrderId(int orderId) {
		deleteByWhere("orderId=" + orderId);
	}

	public void saveOrderItem(OrderItem orderItem, String operation) throws Exception {
		if (operation.equals("del")) {
			super.delete(orderItem.getId());
		} else if (operation.equals("add")) {
			orderItem.setId(null);
			super.save(orderItem);
		} else if (operation.equals("edit")) {
			super.save(orderItem);
		}
		orderManager.updateTotal(orderItem.getOrderId());
	}

	public void updateOrderId(Integer oldOrderId, Integer newOrderId) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery("update " + OrderItem.class.getName() + " set orderId = " + newOrderId + " where orderId = "
		        + oldOrderId);
		query.executeUpdate();
	}

	public int importToPurchase(Integer orderId, Integer purchaseId) {
		List<OrderItem> orderItems = getOrderItemsByOrderId(orderId);
		for (OrderItem item : orderItems) {
			PurchaseItem purchaseItem = new PurchaseItem();
			purchaseItem.setName("");
			purchaseItem.setEnglishName(item.getProductDecription());
			purchaseItem.setQuantity(item.getQuantity());
			purchaseItem.setCustomerPurchaseNo(item.getCustomerPurchaseNo());
			purchaseItem.setPurchaseId(purchaseId);
			purchaseItem.setUnit(item.getUnit());
			purchaseItem.setUnitPrice(0D);
			purchaseItemManager.save(purchaseItem);
		}
		return orderItems.size();
	}

	public int importToPacking(Integer[] orderIds, Integer packingId) {
		int total = 0;
		for (Integer orderId : orderIds) {
			List<OrderItem> orderItems = getOrderItemsByOrderId(orderId);
			for (OrderItem item : orderItems) {
				Double packed = packingItemManager.getTotalPackingQuantityOfOrderItem(item.getObjectId());
				if (packed == null || item.getQuantity() > packed) {
					PackingItem packingItem = new PackingItem();
					packingItem.setDescription(item.getProductDecription());
					packingItem.setPackingId(packingId);
					packingItem.setQuantity(item.getQuantity() - (packed == null ? 0 : packed));
					packingItem.setPackages(0);
					packingItem.setGrossWeight(0D);
					packingItem.setMeasurement(0D);
					packingItem.setNetWeight(0D);
					packingItem.setPalletNo("");
					packingItem.setProductNo(item.getCustomerPurchaseNo());
					packingItem.setUnit(item.getUnit());
					packingItem.setOrderItemId(item.getObjectId());
					packingItem.setPackageUnit(0D);
					packingItemManager.save(packingItem);
					total++;
				}
			}
		}
		if (total > 0) {
			orderAssociatedManager.savePackingOrders(packingId);
		}
		return total;
	}

	public int importToCommercialInvoice(Integer[] orderIds, Integer commercialInvoiceId) {
		int total = 0;
		for (Integer orderId : orderIds) {
			List<OrderItem> orderItems = getOrderItemsByOrderId(orderId);
			for (OrderItem item : orderItems) {
				Double packed = commercialInvoiceItemManager.getTotalPackingQuantityOfOrderItem(item.getObjectId());
				if (packed == null || item.getQuantity() > packed) {
					CommercialInvoiceItem commercialInvoiceItem = new CommercialInvoiceItem();
					commercialInvoiceItem.setProductDecription(item.getProductDecription());
					commercialInvoiceItem.setCommercialInvoiceId(commercialInvoiceId);
					commercialInvoiceItem.setQuantity(item.getQuantity() - (packed == null ? 0 : packed));
					commercialInvoiceItem.setUnit(item.getUnit());
					commercialInvoiceItem.setUnitPrice(item.getUnitPrice());
					commercialInvoiceItem.setCustomerPerchaseNo(item.getCustomerPurchaseNo());
					commercialInvoiceItem.setOrderItemId(item.getObjectId());
					commercialInvoiceItemManager.save(commercialInvoiceItem);
					total++;
				}
			}
		}
		if (total > 0) {
			orderAssociatedManager.saveCommercialInvoiceOrders(commercialInvoiceId);
		}
		return total;
	}

}
